﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using Microsoft.Phone.Shell;

namespace HomeWork3
{
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        PhoneApplicationService appPhoneService = PhoneApplicationService.Current;


        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            myTextBox.Visibility = Visibility.Visible;
            var appStorag = IsolatedStorageFile.GetUserStoreForApplication();
            string fileName = "simple.txt";
            if (appStorag.FileExists(fileName))
            {
                using (System.IO.StreamReader sr = new System.IO.StreamReader(appStorag.OpenFile(fileName, System.IO.FileMode.Open)))
                {
                    this.myTextBox.Text = sr.ReadToEnd();
                }
            }
            myTextBlock.Visibility = Visibility.Collapsed;
            myTextBox.Focus();

        }

        private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
        {
            var appStorage = IsolatedStorageFile.GetUserStoreForApplication();
            string fileName = "simple.txt";

            using (var file = appStorage.OpenFile(fileName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
            {
                using (var writter = new System.IO.StreamWriter(file))
                {
                    writter.Write(myTextBox.Text);
                }
            }
            myTextBlock.Text = myTextBox.Text;
            myTextBox.Visibility = Visibility.Collapsed;
            myTextBlock.Visibility = Visibility.Visible;


        }

        private void ApplicationBarIconButton_Click_2(object sender, EventArgs e)
        {
            var appStorage = IsolatedStorageFile.GetUserStoreForApplication();
            string fileName = "simple.txt";

            if(appStorage.FileExists(fileName))
            {
                appStorage.DeleteFile(fileName);
            }

            myTextBlock.Text = "";
            myTextBox.Text = "";
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            var appStorag = IsolatedStorageFile.GetUserStoreForApplication();
            string fileName = "simple.txt";
            if (appStorag.FileExists(fileName))
            using (System.IO.StreamReader sr = new System.IO.StreamReader(appStorag.OpenFile(fileName, System.IO.FileMode.Open)))
            {
                this.myTextBlock.Text = sr.ReadToEnd();
            }
            myTextBlock.Visibility = Visibility.Visible;
        }
    }
}
